<?php

Raise::load('core.helper.convert.RaiseUnitConverter');

/**
 * RaiseJsonConverter class
 * for conversion between JSON string and array data
 * 
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.helper.convert.unit
 * @since 1.2
 */
class RaiseJsonConverter extends RaiseUnitConverter {
    
    /**
     * Encode Operation
     */
    const ENCODE = 0;
    
    /**
     * Decode Operation
     */
    const DECODE = 1;
    
    /**
     * Encode/Decode the subject to/from JSON string
     * @param mixed $subject
     * @return mixed 
     */
    public function convert($subject) {
        if($this->to() == self::ENCODE){
            return json_encode($subject);
        }else{
            return new RaiseCollection(json_decode($subject, true));
        }
    }
    
    public static function encode($subject){
        $converter = new self(self::DECODE, self::ENCODE);
        return $converter->convert($subject);
    }
    
    public static function decode($subject){
        $converter = new self(self::ENCODE, self::DECODE);
        return $converter->convert($subject);
    }
    
}